package
{
	import Hxw3DEngine.asset.AssetManage;
	import Hxw3DEngine.asset.loader.AssetLoader;
	import Hxw3DEngine.asset.loader.AssetQueueLoader;
	import Hxw3DEngine.asset.parsers.ObjParser;
	import Hxw3DEngine.asset.parsers.ParserEvent;
	import Hxw3DEngine.core.Resource.geometry.ResourceGeometry;
	import Hxw3DEngine.core.Resource.texture.BitmapTexture;
	import Hxw3DEngine.core.component.renderer.MeshRenderer;
	import Hxw3DEngine.core.controller.View3D;
	import Hxw3DEngine.core.display.GameObject;
	import Hxw3DEngine.core.display.Panel3D;
	import Hxw3DEngine.core.display.SkyBox;
	import Hxw3DEngine.core.lights.DirectionalLight;
	import Hxw3DEngine.core.materials.TextureMaterial;
	import Hxw3DEngine.core.materials.shader.newFilter.DirLightFilter;
	import Hxw3DEngine.utils.Color;
	import Hxw3DEngine.utils.FPS;
	import Hxw3DEngine.utils.Stage3dUtils;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Vector3D;
	
	
	//测试灯光的demo 
	[SWF(width="800",height="600",frameRate="60")]
	public class TestLightDemo
	{
		private var urlPrefix:String;
		private var sprite:Sprite;

		private var urlEnvPosX:String ="assets/skybox/snow/px.jpg";
		private var urlEnvPosY:String ="assets/skybox/snow/py.jpg";
		private var urlEnvPosZ:String ="assets/skybox/snow/pz.jpg";
		private var urlEnvNegX:String ="assets/skybox/snow/nx.jpg";
		private var urlEnvNegY:String ="assets/skybox/snow/ny.jpg";
		private var urlEnvNegZ:String ="assets/skybox/snow/nz.jpg";
		
		private var floorUrl:String = "assets/floor/3.jpg";
		
		//测试光照
		private var testLightDiffuseUrl:String = "assets/obj/head/diffuse.jpg"
		
		private var testLightObjUrl:String = "assets/obj/head/obj.obj";
		
		private var view:View3D;
		
		public function TestLightDemo(_sprite:Sprite,_urlPrefix:String)
		{
			urlPrefix = _urlPrefix;
			sprite = _sprite;
			AssetLoader.url_prefix = urlPrefix + "/";

			//init engine
			view = new View3D();
			sprite.addChild(view);
			view.camera.setPosition(0,0,0);
			view.camera.lookAt(new Vector3D(0,0,0),new Vector3D(0,10,-25),new Vector3D(0,1,0));
			
			
			//队列加载资源			
			var res:Array = [
				urlEnvPosX,
				urlEnvPosY,
				urlEnvPosZ,
				urlEnvNegX,
				urlEnvNegY,
				urlEnvNegZ,		
				
				testLightDiffuseUrl,
				testLightObjUrl,
			
				floorUrl,
			];
			var loder:AssetQueueLoader = new AssetQueueLoader(res,initThis);
			loder.startLoad();
			
		}
		
		//obj解析器
		private var parser:ObjParser;
		
		private function initThis():void {
			
			//帧显示
			sprite.addChild(FPS.insdance);
			
			//一个天空盒
			var skybox:SkyBox = new SkyBox(
						Stage3dUtils.bitmapData(AssetManage.getAsset(urlEnvPosX)), 
						Stage3dUtils.bitmapData(AssetManage.getAsset(urlEnvNegX)),
						Stage3dUtils.bitmapData(AssetManage.getAsset(urlEnvPosY)), 
						Stage3dUtils.bitmapData(AssetManage.getAsset(urlEnvNegY)), 
						Stage3dUtils.bitmapData(AssetManage.getAsset(urlEnvPosZ)), 
						Stage3dUtils.bitmapData(AssetManage.getAsset(urlEnvNegZ))
						);
			//view.scene.addChild(skybox);
			
			
			//一个地面
			var p:Panel3D = new Panel3D(new TextureMaterial(BitmapTexture.create(Stage3dUtils.bitmapData(AssetManage.getAsset(floorUrl)), true)),10,10,2);
			//view.scene.addChild(p);
			
			//坐标系
			//view.scene.addChild(new Coordinate());
			
			sprite.stage.addEventListener(MouseEvent.MOUSE_WHEEL,onWheel);
			sprite.stage.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
			sprite.stage.addEventListener(MouseEvent.MOUSE_MOVE,onMove);
			sprite.stage.addEventListener(MouseEvent.MOUSE_UP,onUp);	
			sprite.addEventListener(Event.ENTER_FRAME,onEnter);
			
			parser= new ObjParser();
			parser.addEventListener(ParserEvent.PARSERCOMPLETE,onLightObject);
			parser.startParser(AssetManage.getAsset(testLightObjUrl));
		}
		
		private function onLightObject(e:Event):void{
			var obj:GameObject = new GameObject();
			var meshRender:MeshRenderer = new MeshRenderer();
			obj.addComponent(meshRender);
			meshRender.geometry = new ResourceGeometry();
			
			meshRender.geometry.createFormVectors(parser.numVertices,parser.vertexData,parser.indexData,parser.uvData,parser.normalData,parser.tangentData);
			meshRender.material = new TextureMaterial(BitmapTexture.create(Stage3dUtils.bitmapData(AssetManage.getAsset(testLightDiffuseUrl)), true))
			
			var light:DirectionalLight = new DirectionalLight();
			light.color = new Color(0xff0000);
			light.power = 100;
			light.transform.setPosition(0,50,0);
			
			meshRender.material.shader.addFilter(new DirLightFilter(light));
			obj.transform.setPosition(0,3, -5);
			obj.transform.setRotation(0,180,0);
			view.scene.addChild(obj);
		}
		
		
		//操作相机
		private function onWheel(e:MouseEvent):void{
			view.camera.back(-e.delta);
		}
		
		private var start:Boolean;
		private var mousex:Number;
		private var mousey:Number;
		private function onDown(e:MouseEvent):void{
			start = true;
			mousex = sprite.stage.mouseX;
			mousey = sprite.stage.mouseY;
		}
		
		private function onUp(e:MouseEvent):void{
			start = false;
		}
		
		private var _ang:Number = 0;
		private function onMove(e:MouseEvent):void{
			if(start){
				var dx:Number = (sprite.stage.mouseX - mousex)*0.2;
				var dy:Number = (sprite.stage.mouseY - mousey)*0.2;
				view.camera.move(dx,dy);
			}
		}
		
		
		private function onEnter(e:Event):void{
			//if(view)view.camera.move(2,1);
		}
		
		
	}
}